﻿/*
 声音进度条录制时需要；
 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using System.Data;
using Common;
using CSharpConnectMySQL;
using MySql.Data.MySqlClient;



namespace L2
{ /// <summary>
  /// 电子看板；。
  /// </summary>
    public static class L2_01Dzdp_Screen
    {
      
        public static string  Set_Dzdp_Screen01(string ip)
        {
            string ps_name = "Set_Dzdp_Screen01";
            string msg = "-1";//表示异常；  17E10
            int i = 0;


            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;

            DataSet dataSet; //临时表结构



            ClassMyFile.WriteLog("begin--------------------------------【"+ ip + "】 ", ps_name);


            //1图片打开识别；-------------------------------------
          

            string imageUrl = ClassL2Ai.get_imageUrl(ip);

            //防止图片打开错误，共执行3次；每次间隔1.2秒；
            i = 1;
            while  (imageUrl=="-1" &&  i<3)
            {
                i++;
                System.Threading.Thread.Sleep(1200);
                imageUrl = ClassL2Ai.get_imageUrl(ip);
            }

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;


            ClassMyFile.WriteLog(string.Format("get_imageUrl,耗时{0}ms, 第[{1}]次拍照", subTime, i), ps_name);

            dt1 = DateTime.Now;


            if (imageUrl == "-1")
            {  
                msg = string.Format("-1 get_imageUrl，照片抓取识别,IP{0}, imageUrl[{1}]", ip, imageUrl);
                ClassMyFile.WriteLog(msg, ps_name);
                return msg;
            }
           
            


            string str_num = ClassL2Ai.get_imageOcr(imageUrl, "C");

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;

            ClassMyFile.WriteLog(string.Format("get_imageOcr,耗时[{0}]ms, C型钩[{1}]", subTime, str_num), ps_name);
            dt1 = DateTime.Now;

            if (str_num.Contains("-1"))
            {
                msg = string.Format("-1 get_imageOcr，图形识别,IP{0}, imageUrl[{1}]", ip, imageUrl);
                ClassMyFile.WriteLog(msg, ps_name);
                return msg;
            }
           



            msg = Set_Dzdp_Screen02(ip, str_num);

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;

            ClassMyFile.WriteLog(string.Format("Set_Dzdp_Screen02,耗时[{0}]ms, ", subTime), ps_name);
            dt1 = DateTime.Now;



            if (msg.Contains("-1"))
            {
                msg = string.Format("-1 电子大屏发生数据错误[{0}", msg);
                ClassMyFile.WriteLog(msg, ps_name);
                return msg;
            }
            else
            {
                msg = "C型钩[" + str_num + "]," + msg;
            }





            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;

            ClassMyFile.WriteLog("end ------------------------------------------总耗时[" + subTime.ToString() + "] ", ps_name);





            return msg;


        }



        //用于调试
        public static string Set_Dzdp_Screen02(string ip,string hanger_number)
        {
            string ps_name = "Set_Dzdp_Screen02";
            string msg = "-1";//表示异常；  17E10
            int i = 0;


            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;
            DataSet dataSet; //临时表结构



           
            ClassMyFile.WriteLog("begin--------------------------------【" + ip + "】 ", ps_name);


            string sql = "  select flow_id, serial_number, hanger_number, set_hanger_time   from t_material where "
                       + "   (flow_id = 'B_GuaJuanZhuangZhi' or flow_id = 'A_GuaJuanZhuangZhi'  )  "
                       + "  and hanger_number = '" + hanger_number + "' "
                       + "  order by  set_hanger_time desc ";

            dataSet = MySqlHelper2.GetDataSet(sql);
            if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
            {
                msg = string.Format("-1 当前无数据,sql:\n{0}", sql);
                ClassMyFile.WriteLog(msg, ps_name);
                return msg;
            }


            string str_set_hanger_time = dataSet.Tables[0].Rows[0]["set_hanger_time"].ToString();

           


            //sg_number 为识别钢种；
            sql = "  select hanger_number hangerNumber , batch_number batchNumber , mat_spec_desc matSpecDesc, sg_sign  sgSign  from t_material where "
                      + "   (flow_id = 'B_GuaJuanZhuangZhi' or flow_id = 'A_GuaJuanZhuangZhi'  )  "
                      + "  and set_hanger_time >= '" + str_set_hanger_time + "' "
                      + "  order by  set_hanger_time   LIMIT 0, 20 ";

            dataSet = MySqlHelper2.GetDataSet(sql);
            if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
            {
                msg = string.Format("-1 当前无数据,sql:\n{0}", sql);
                ClassMyFile.WriteLog(msg, ps_name);
                return msg;
            }



            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("数据查找耗时{0}ms", subTime), ps_name);
            dt1 = DateTime.Now;



            string jsonString = "";

            //jsonString = JsonConvert.SerializeObject(dataSet, Formatting.Indented); 也是可以的；
            jsonString = JsonConvert.SerializeObject(dataSet.Tables[0], Formatting.Indented);




            //host = "http://192.168.121.242/api/main/flow/send_data_to_screen/quYangDian2";

            string host = "http://192.168.121.248/api/main/flow/send_data_to_screen/";

           // host = "http://192.168.121.248/api/main/flow/send_data_to_screen/";

            if (ip== "10.233.121.10")
            {
                host = host + "quYangDian2";
            }
            else if (ip == "10.233.121.11")
            {
                host = host + "quYangDian1";
            }
            


            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("materialListData", jsonString);

            msg = MyHttp.http_post_mf(ps_name, host, parameters);

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;

            ClassMyFile.WriteLog(string.Format("调用大屏接口,耗时{0}ms", subTime), ps_name);
            dt1 = DateTime.Now;


            if (msg.Contains("-1"))
            {
                msg = "-1 调用大屏接口错误";
                ClassMyFile.WriteLog(msg, ps_name);
            } 
            else
            {
                msg = "0 发送大屏成功，共["+ dataSet.Tables[0].Rows.Count.ToString() + "]条记录";
            }


            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;


            ClassMyFile.WriteLog("end ------------------------------------------总耗时["+ subTime.ToString()+ "] ", ps_name);


            return msg;


        }



    }


 


}
